/*
 * Created by JFormDesigner on Mon Aug 01 15:01:21 CST 2022
 */

package client.login;

import client.fileMange.FileMange;
import client.util.ClientConn;
import command.Command;
import command.login.LoginCommand;
import empty.users.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author unknown
 */
public class Login extends JFrame {
    public Login() {
        initComponents();
    }

    private void btn_login(ActionEvent e) {
        String name = this.text_account.getText();
        String pwd = this.text_pwd.getText();
        if (name.equals("") || pwd.equals("")) {
            JOptionPane.showMessageDialog(this, "输入不能为空");
        } else {
            Command comm = new LoginCommand();
            comm.setO(new User(name, pwd));
            try {
                ClientConn.oos.writeObject(comm);
                comm = (Command) ClientConn.ois.readObject();
                if((Boolean) comm.getR()){
                    ClientConn.user = (User) comm.getO();
                    JOptionPane.showMessageDialog(this,"登录成功");

                    this.setVisible(false);
                    // 打开新窗口
                    // 管理员账户
                    if(ClientConn.user.getManger() == 1){
                        Main main = new Main();
                        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        main.setVisible(true);
                    }
                    // 普通用户
                    else {
                        FileMange fileMange = new FileMange();
                        fileMange.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        fileMange.setVisible(true);
                    }
                }
                else{
                    if(comm.getE()!=null){
                        JOptionPane.showMessageDialog(this,"服务器错误："+comm.getE());
                    }
                    else{
                        JOptionPane.showMessageDialog(this,"用户名或密码错误");
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private void btn_quite(ActionEvent e) {
        this.dispose();
    }

    private void text_accountKeyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            this.btn_login.doClick();
        }
    }

    private void text_pwdKeyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            this.btn_login.doClick();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label1 = new JLabel();
        label2 = new JLabel();
        text_account = new JTextField();
        btn_quite = new JButton();
        btn_login = new JButton();
        text_pwd = new JPasswordField();

        //======== this ========
        setResizable(false);
        setTitle("\u767b\u5f55");
        var contentPane = getContentPane();

        //======== panel1 ========
        {
            panel1.setBackground(Color.white);

            //---- label1 ----
            label1.setText("\u7528\u6237\u540d");
            label1.setHorizontalAlignment(SwingConstants.CENTER);
            label1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 18));

            //---- label2 ----
            label2.setText("\u5bc6\u7801");
            label2.setHorizontalAlignment(SwingConstants.CENTER);
            label2.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 18));

            //---- text_account ----
            text_account.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
            text_account.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    text_accountKeyPressed(e);
                }
            });

            //---- btn_quite ----
            btn_quite.setText("\u53d6\u6d88");
            btn_quite.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
            btn_quite.addActionListener(e -> btn_quite(e));

            //---- btn_login ----
            btn_login.setText("\u767b\u5f55");
            btn_login.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));
            btn_login.addActionListener(e -> btn_login(e));

            //---- text_pwd ----
            text_pwd.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    text_pwdKeyPressed(e);
                }
            });

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(180, 180, 180)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(btn_quite, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
                                .addGap(109, 109, 109)
                                .addComponent(btn_login))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label2, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                    .addComponent(text_account, GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                                    .addComponent(text_pwd))))
                        .addContainerGap(212, Short.MAX_VALUE))
            );
            panel1Layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {btn_login, btn_quite});
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
                            .addComponent(text_account, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                        .addGap(38, 38, 38)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addComponent(label2, GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                            .addComponent(text_pwd))
                        .addGap(80, 80, 80)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(btn_quite, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn_login))
                        .addContainerGap(88, Short.MAX_VALUE))
            );
            panel1Layout.linkSize(SwingConstants.VERTICAL, new Component[] {label1, text_account});
            panel1Layout.linkSize(SwingConstants.VERTICAL, new Component[] {btn_login, btn_quite});
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label1;
    private JLabel label2;
    private JTextField text_account;
    private JButton btn_quite;
    private JButton btn_login;
    private JPasswordField text_pwd;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    public static void main(String[] args) throws Exception {
        Login login = new Login();
        String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
        UIManager.setLookAndFeel(lookAndFeel);
        login.setVisible(true);
        login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 连接
        ClientConn.connect();
    }
}
